<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:fn="http://www.w3.org/2005/xpath-functions"
   xmlns="http://www.w3.org/1999/xhtml"
   xmlns:xf="http://www.w3.org/2002/xforms"
   xmlns:ev="http://www.w3.org/2001/xml-events"
   xmlns:u="http://niem.gov/niem/universal/1.0" 
   xmlns:c="http://niem.gov/niem/common/1.0" 
   xmlns:mn="http://data.state.mn.us" 
   xmlns:mnr="http://revenue.state.mn.us" 
   xmlns:mnr-ptx="http://propertytax.state.mn.us" 
   xmlns:s2x="http://schema2xforms.revenue.state.mn.us"   
   >
<!-- Transform an XML Schema into an XForms instance editor. -->
<!-- Dan McCreary -->
<!-- 12/11/2006 -->
<!-- this can be generated from the metadata registry -->
<xsl:import href="screen-labels.xml"/>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" extension-element-prefixes="u c mn mnr mnr-ptx" exclude-result-prefixes="xs"/>
<xsl:template match="/">
<html
   xmlns="http://www.w3.org/1999/xhtml"
   xmlns:xf="http://www.w3.org/2002/xforms"
   xmlns:ev="http://www.w3.org/2001/xml-events"
   xmlns:xs="http://www.w3.org/2001/XMLSchema" 
   xmlns:u="http://niem.gov/niem/universal/1.0"
   xmlns:c="http://niem.gov/niem/common/1.0"
   xmlns:mn="http://data.state.mn.us"
   xmlns:mnr="http://revenue.state.mn.us"
   xmlns:mnr-ptx="http://propertytax.state.mn.us"
>
   <head>
      <title>XForms application generated from constraint schema.</title>
      
      <!-- generic stylesheet used by XForms -->
      <link rel="stylesheet" type="text/css" href="../resources/css/xforms-global.css" />
      <link rel="stylesheet" type="text/css" href="../resources/css/xforms-submit.css" />
       
      <xf:model>
         <xf:instance xmlns="" id="default-instance" src="instance.xml"/>
          <xsl:call-template name="binds-for-required-fields"/>
          <xsl:call-template name="binds-for-dates"/>
          <xsl:call-template name="binds-for-booleans"/>
           <xsl:call-template name="import-code-tables"/>
         <xf:submission id="save" method="post" action="http://localhost:8080/db/contacts/save-contact.xq" instance="default-instance"/>
      </xf:model>
   </head>
   <body>
      <p>This form was automatically generated by an XSL transform from the constraint schema. v0.2</p>
       <xsl:apply-templates select="*"/>
       <img src="../resources/images/XForms-powered.png" alt="XForms Powered"/>
   </body>
</html>   
</xsl:template>

   <xsl:template match="xs:element[@name]|xs:element[@ref]">
      <xsl:variable name="element-name" select="concat(@name, @ref)" as="xs:string"/>
      <xsl:choose>
         <xsl:when test="ends-with($element-name, 'Document')">
         <xf:group>
            <xsl:attribute name="ref" select="concat('/', $element-name)"/>
            <xf:label class="group-label"><xsl:value-of select=" $element-name"/></xf:label>
           <xsl:apply-templates/>
            <xf:submit submission="save">
               <xf:label>Save <xsl:value-of select="substring-before($element-name, 'Document')"/></xf:label>
            </xf:submit>
        </xf:group>
      </xsl:when>
         <xsl:when test="$element-name">
         <xf:group>
         <xsl:attribute name="ref" select="@name"/>
            <xf:label>
               <xsl:attribute name="class">group-label</xsl:attribute>
               <xsl:value-of select="@name"/>
            </xf:label>
               
            <xsl:if test="@type">
               <xsl:variable name="type" select="@type"/>
               <xsl:apply-templates select="//xs:complexType[@name=$type]">
                  <xsl:with-param name="in-element" select="'true'"/>
               </xsl:apply-templates>
               </xsl:if>
               <xsl:apply-templates select="*"/>
            </xf:group>
         </xsl:when>
         <xsl:when test="@ref">
           <xsl:call-template name="leaf-node">
              <xsl:with-param name="leaf-name" select="@ref"/>
           </xsl:call-template>
         </xsl:when>
      </xsl:choose>
</xsl:template>

<xsl:template match="xs:complexType[@name]">
   <xsl:param name="in-element"/>
   <xsl:if test="$in-element='true()'">
   </xsl:if>
      <xsl:apply-templates select="xs:sequence/xs:element|xs:choice/xs:element"/>
</xsl:template>

<xsl:template match="xs:documentation">
</xsl:template>

<xsl:template match="xs:element/@name">
   <xsl:choose>
      <xsl:when test="../xs:annotation/xs:appinfo/s2x:xforms-tab-label">
         <div class="menu-tab"><xsl:value-of select="."/></div>
      </xsl:when>
   <xsl:otherwise>
       <xf:group>
          <xsl:attribute name="ref" select="@name"/>
          <xs:label class="group-box">
             <xsl:value-of select="."/>
         </xs:label>
      </xf:group>
   </xsl:otherwise>
      </xsl:choose>
</xsl:template>

<xsl:template name="leaf-node">
   <xsl:param name="leaf-name" as="xs:string" required="yes"/>
   <!-- <xsl:comment><xsl:value-of select="$leaf-name"/></xsl:comment> -->
   <xsl:choose>
      <xsl:when test="ends-with($leaf-name, 'Amount')">
         <xf:input>
            <xsl:attribute name="ref"><xsl:value-of select="$leaf-name"/></xsl:attribute>
            <xf:label><xsl:value-of select="$lookup-labels/data/item[from=substring-after($leaf-name,':')]/to"/>: </xf:label>
         </xf:input>
      </xsl:when>
   <xsl:when test="ends-with($leaf-name, 'Code')">
      <xf:select1>
          <xsl:attribute name="ref"><xsl:value-of select="$leaf-name"/></xsl:attribute>
         <xf:label><xsl:value-of select="$lookup-labels/data/item[from=substring-after($leaf-name,':')]/to"/>: </xf:label>
         <xf:itemset model="code-tables">
            <xsl:attribute name="nodeset">instance('<xsl:value-of select="$leaf-name"/>')/EnumeratedValues/Item</xsl:attribute>
            <xf:label ref="Label" />
            <xf:value ref="Value" />
         </xf:itemset>
      </xf:select1>
   </xsl:when>
    <xsl:when test="ends-with($leaf-name, 'Count')">
         <xf:input>
            <xsl:attribute name="ref"><xsl:value-of select="$leaf-name"/></xsl:attribute>
            <xf:label><xsl:value-of select="$lookup-labels/data/item[from=substring-after($leaf-name,':')]/to"/>: </xf:label>
         </xf:input>
      </xsl:when>
   <xsl:when test="ends-with($leaf-name, 'Date')">
      <xf:input>
         <xsl:attribute name="ref" select="$leaf-name"/>
         <xf:label><xsl:value-of select="$lookup-labels/data/item[from=substring-after($leaf-name,':')]/to"/>: </xf:label>
      </xf:input>
   </xsl:when>
   <xsl:when test="ends-with($leaf-name, 'DateTime')">
      <xf:input>
         <xsl:attribute name="ref" select="$leaf-name"/>
         <xf:label><xsl:value-of select="$lookup-labels/data/item[from=substring-after($leaf-name,':')]/to"/>: </xf:label>
      </xf:input>
   </xsl:when>
   <xsl:when test="ends-with($leaf-name, 'Document')">
   </xsl:when>
   <xsl:when test="ends-with($leaf-name, 'ID')">
      <xf:input>
         <xsl:attribute name="ref" select="$leaf-name"/>
         <xf:label><xsl:value-of select="$lookup-labels/data/item[from=substring-after($leaf-name,':')]/to"/>: </xf:label>
      </xf:input>
      </xsl:when>
   <xsl:when test="ends-with($leaf-name, 'Indicator')">
      <xf:select1 appearance="full">
         <xsl:attribute name="ref" select="$leaf-name"/>
         <xf:label><xsl:value-of select="$lookup-labels/data/item[from=substring-after($leaf-name,':')]/to"/>: </xf:label>
         <xf:item>
            <xf:label>Yes</xf:label>
            <xf:value>yes</xf:value>
         </xf:item>
         <xf:item>
            <xf:label>No</xf:label>
            <xf:value>no</xf:value>
         </xf:item>
      </xf:select1>
   </xsl:when>
   <xsl:when test="ends-with($leaf-name, 'Measure')">
      <xf:input>
         <xsl:attribute name="ref" select="$leaf-name"/>
         <xf:label><xsl:value-of select="$lookup-labels/data/item[from=substring-after($leaf-name,':')]/to"/>: </xf:label>
      </xf:input>
      </xsl:when>
   <xsl:when test="ends-with($leaf-name, 'Name')">
      <xf:input>
            <xsl:attribute name="ref" select="$leaf-name"/>
            <xf:label><xsl:value-of select="$lookup-labels/data/item[from=substring-after($leaf-name,':')]/to"/>: </xf:label>
      </xf:input>
   </xsl:when>
   <xsl:when test="ends-with($leaf-name, 'Percent')">
      <xf:input>
            <xsl:attribute name="ref" select="$leaf-name"/>
            <xf:label><xsl:value-of select="$lookup-labels/data/item[from=substring-after($leaf-name,':')]/to"/>: </xf:label>
         </xf:input>
      </xsl:when>
   <xsl:when test="ends-with($leaf-name, 'TaxRate')">
      A <b>Tax Rate</b> textfield
   </xsl:when>
   <!-- TODO: when should we use small, medium and large textfields?  Where should this be kept? -->
   <xsl:when test="ends-with($leaf-name, 'Text')">
      <xf:textarea class="medium">
            <xsl:attribute name="ref" select="$leaf-name"/>
            <xf:label><xsl:value-of select="$lookup-labels/data/item[from=substring-after($leaf-name,':')]/to"/>: </xf:label>
      </xf:textarea>
   </xsl:when>
   <xsl:when test="ends-with($leaf-name, 'Year')">
      <xf:input>
            <xsl:attribute name="ref" select="$leaf-name"/>
            <xf:label><xsl:value-of select="$lookup-labels/data/item[from=substring-after($leaf-name,':')]/to"/>: </xf:label>
         </xf:input>
      </xsl:when>
   <xsl:otherwise>
      <b><font color="red">Error</font></b>
   </xsl:otherwise>
   </xsl:choose>
   
   <xsl:text>
   </xsl:text>
</xsl:template>

    <xsl:template name="import-code-tables">
		<xsl:for-each select="//xs:element/@ref[matches(.,'Code$')]">
		<xf:instance xmlns="">
			<xsl:variable name="unqualified-name" select="substring-after(., ':')" as="xs:string"/>
			<xsl:attribute name="id" select="$unqualified-name"/>
			<xsl:attribute name="src" select="concat('../resources/code-tables/', $unqualified-name, '.xml')"/>
		</xf:instance>
		</xsl:for-each>
    </xsl:template>
   
   <!-- add bindings for all required fields -->
   <xsl:template name="binds-for-required-fields">
   <!-- for all elements that have a @ref but no @minOccurs -->
   <xsl:for-each select=" //xs:element[@ref and not( @minOccurs)]">
      <xf:bind>
         <xsl:attribute name="nodeset" select="concat('//', @ref)"/>
         <xsl:attribute name="required" select="'true()'"/>
      </xf:bind>
   </xsl:for-each>
   </xsl:template>
   
   <!-- add bindings for all dates -->
   <xsl:template name="binds-for-dates">
   <!-- for all elements that have a Date suffix -->
   <xsl:for-each select=" //xs:element[ends-with(@ref, 'Date')]">
      <xf:bind>
         <xsl:attribute name="nodeset" select="concat('//', @ref)"/>
         <xsl:attribute name="type" select="'xs:date'"/>
      </xf:bind>
   </xsl:for-each>
   </xsl:template>
   
   <!-- add bindings for booleans -->
   <xsl:template name="binds-for-booleans">
   <!-- for all elements that have a Date suffix -->
   <xsl:for-each select=" //xs:element[ends-with(@ref, 'Indicator')]">
      <xf:bind>
         <xsl:attribute name="nodeset" select="concat('//', @ref)"/>
         <xsl:attribute name="type" select="'xs:boolean'"/>
      </xf:bind>
   </xsl:for-each>
   </xsl:template>

</xsl:stylesheet>
